<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/6/27
 * Time: 7:53
 */
namespace app\adminapi\validate;

class TemplateUpdate extends Base
{
    protected $rule = [
        'id' => 'require|integer|min:1|dataExists',
        'name' => 'require|nameUnique',
        'src' => 'require',
    ];

    protected $message = [
        'id.dataExists' => '数据不存在',

        'name.nameUnique' => '基础模板名唯一',
    ];

    /**
     * 数据是否存在
     * @param $value
     * @return bool
     * @throws \think\Exception
     */
    protected function dataExists($value)
    {
        if(!db('template')->where("id={$value}")->count()) return false;
        return true;
    }

    /**
     * 基础模板名唯一
     * @param $value
     * @return bool
     * @throws \think\Exception
     */
    protected function nameUnique($value, $rule, $data)
    {
        if(db('template')->where("name='{$value}' and id!={$data['id']}")->count()) return false;
        return true;
    }

}